package com.xiaoKe.zhonghzu.global.okhttp;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.orhanobut.logger.Logger;
import com.xiaoKe.zhonghzu.global.tool.AppConstant;
import com.xiaoKe.zhonghzu.global.tool.SPUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import okio.Buffer;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @ProjectName : xiaoke-pad-zhongzhu
 * @Author : Shijingna
 * @Time : 2024/7/12 15:23
 * @Description : 接口
 */
public class RetrofitClientLogin {
    private static Retrofit retrofitLogin = null;

    public static Retrofit getClientLogin() {
        if (retrofitLogin == null) {
            // 创建 Retrofit 实例
            Gson gson = new GsonBuilder().setLenient().create();
            retrofitLogin = new Retrofit.Builder()
                    .baseUrl(AppConstant.URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .client(okHttp())
                    .build();
        }
        return retrofitLogin;
    }


    public static OkHttpClient okHttp() {
        // 配置日志拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // 创建 OkHttpClient 实例
        return new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request original = chain.request();
                        // 添加请求头部
                        Request.Builder requestBuilder = original.newBuilder();
                        Request request = requestBuilder.build();
                        Response response = chain.proceed(request);

                        // 检查响应状态码
                        if (response.code() == 401) {
                            // 记录 401 错误日志
                            Logger.e("Unauthorized! Status code: " + response.code());
                            // 可以抛出异常或其他错误处理

                        }
                        if (AppConstant.IS_DEBUG) {
                            String result = response.body() != null ? response.body().string() : null;
                            // 打印请求体
                            RequestBody requestBody = request.body();
                            Logger.e("完整请求(url = %s)", request.url());
                            if (requestBody != null) {
                                Buffer buffer = new Buffer();
                                requestBody.writeTo(buffer);
                                Logger.json(buffer.readUtf8());
                            }
                            Logger.e("响应(%s)", request.header("tag"));
                            Logger.json(result);
                            // 需要重新创建响应体才能返回
                            response = response.newBuilder().body(okhttp3.ResponseBody.create(response.body().contentType(), result)).build();
                        }
                        return response;
                    }
                }).build();
    }
}